/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-10-19
*/
package com.rzico.business.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.business.entity.KkxxInfo;
import com.rzico.business.service.KkxxInfoService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 卡口信息控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-10-19
 */
@Api(description = "卡口信息接口")
@RestController
@RequestMapping("/admin/kkxxInfo")
public class KkxxInfoController extends BaseController {

    @Autowired
    private KkxxInfoService kkxxInfoService;

    /**
     * 分页查询卡口信息
     *
     * @return
    */
    @ApiOperation("分页查询卡口信息")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "kkbh", value = "卡号编号", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "kkmc", value = "卡号名称", dataType = "String", paramType = "query")
    })
    public CommResult<KkxxInfo> list(String kkbh, String kkmc, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (kkmc!=null){
           params.put("kkmc", kkmc);
        }
        if (kkbh!=null){
            params.put("kkbh", kkbh);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<KkxxInfo> list = kkxxInfoService.selectList(params);
        PageResult<KkxxInfo> pageResult = new PageResult<KkxxInfo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条卡口信息
     *
     * @return
    */
    @ApiOperation("查询单条卡口信息")
    @GetMapping("/find/{id}")
    public CommResult<KkxxInfo> find(@PathVariable String id) {
        KkxxInfo result = kkxxInfoService.selectByPrimaryKey(id);

        Map<String,Object> data = new HashMap<>();
        data.put("kkxxInfo",result);
        return CommResult.success(data);

    }

    /**
     * 保存卡口信息
     *
     * @param kkxxInfo
     * @return
     */
    @ApiOperation("保存卡口信息")
    @PostMapping("/save")
    public CommResult<KkxxInfo> save(@RequestBody KkxxInfo kkxxInfo) {
        kkxxInfo.setCreateDate(new Date());
        kkxxInfo.setModifyDate(new Date());
        kkxxInfo.setModifyBy("");
        kkxxInfo.setCreateBy("");
        int affectCount = kkxxInfoService.insert(kkxxInfo);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        KkxxInfo result = kkxxInfoService.selectByPrimaryKey(kkxxInfo.getKkid());
        return CommResult.success(result);

    }


    /**
     * 批量删除卡口信息
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除卡口信息", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除卡口信息,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<KkxxInfo> del(@PathVariable String ids) {

        kkxxInfoService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新卡口信息
     *
     * @param kkxxInfo
     * @return
     */
    @Log(desc = "更新卡口信息", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新卡口信息")
    @PostMapping("/update")
    public CommResult<KkxxInfo> update(@RequestBody KkxxInfo kkxxInfo) {
        kkxxInfo.setCreateDate(new Date());
        kkxxInfo.setModifyDate(new Date());
        kkxxInfo.setModifyBy("");
        kkxxInfo.setCreateBy("");
        int affectCount = kkxxInfoService.updateByPrimaryKeySelective(kkxxInfo);
        if (affectCount <= 0){
            return CommResult.error();
        }
        KkxxInfo result = kkxxInfoService.selectByPrimaryKey(kkxxInfo.getKkid());
        return CommResult.success(result);
    }

}
